package id.co.shuba.spamobile.activities;

import java.text.NumberFormat;
import java.util.ArrayList;

import id.co.shuba.spamobile.App;
import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.data.ISharedDB;
import id.co.shuba.spamobile.data.StandardDB;
import id.co.shuba.spamobile.data.ProspectDB;
import id.co.shuba.spamobile.model.ProspectInfo;
import id.co.shuba.spamobile.util.DialogUtil;
import id.co.shuba.spamobile.util.Str;
import id.co.shuba.spamobile.widget.GroupAdapter;
import id.co.shuba.spamobile.widget.GroupInfo;
import id.co.shuba.spamobile.widget.IListItem;
import id.co.shuba.spamobile.widget.ListItem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class PipelineViewFragment extends Fragment {
	private TextView txtName;
	private ListView lst;
	private ProspectDB _db;
	private String _prospectID;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);									
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {		
		super.onActivityCreated(savedInstanceState);
		
		_db = new ProspectDB(((ISharedDB)getActivity()).getDB());
		reload();
	}
	
	public void setProspectID(String id) {
		_prospectID = id;
	}
	
	private String numberFormat(int num) {
		NumberFormat nf =
                NumberFormat.getInstance();
		return nf.format(num);
	}
	
	private String numberFormat(long num) {
		NumberFormat nf =
                NumberFormat.getInstance();
		return nf.format(num);
	}
	
	public void reload() {				
		ProspectInfo info = _db.getFullInfo(_prospectID);
		txtName.setText(info.getFullName());		
		ArrayList<IListItem> list = new ArrayList<IListItem>();
		list.add(new GroupInfo("Info"));
		//list.add(new ListItem("ID", info.getId()));
		list.add(new ListItem(getString(R.string.prn_number), info.getProspectNo()));
		list.add(new ListItem("Progress Status", info.getProgressStatus()));
		list.add(new ListItem(getString(R.string.job_type), info.getJenisUsaha()));
		list.add(new ListItem(getString(R.string.plafon_plan), numberFormat(info.getRencanaPlafon())));		
		list.add(new ListItem(getString(R.string.outsiders), info.getEksternal()));
		list.add(new ListItem(getString(R.string.channelling), info.getChanneling()));
		list.add(new ListItem(getString(R.string.reference), info.getReferensi()));
		//list.add(new ListItem(getString(R.string.visit_date), info.getVisitDate()));	
		list.add(new ListItem("Kode Officer", info.getAoCode()));
		list.add(new ListItem("Kode Unit", info.getUnitCode()));
		list.add(new GroupInfo("Address"));
		list.add(new ListItem(getString(R.string.work_address), info.getAddressBusiness()));
		list.add(new ListItem(getString(R.string.district), info.getKodyaKabBusiness()));
		list.add(new ListItem(getString(R.string.sub_district), info.getKecamatanBusiness()));
		list.add(new ListItem(getString(R.string.village), info.getKelurahanBusiness()));
		list.add(new ListItem(getString(R.string.postal_code), info.getZipCodeBusiness()));
		list.add(new GroupInfo("Contact"));
		list.add(new ListItem(getString(R.string.mobile_phone), info.getMobilePhone()));
		list.add(new ListItem(getString(R.string.office_phone), info.getOfficePhone()));
		list.add(new ListItem(getString(R.string.home_phone), info.getResidentialPhone()));
		if ("1".equals(info.getIsPipelineKP())) {
			list.add(new ListItem("Omzet 1", numberFormat(info.getOmzet1())));
			list.add(new ListItem("Omzet 2", numberFormat(info.getOmzet2())));
			list.add(new ListItem("Omzet 3", numberFormat(info.getOmzet3())));
			list.add(new ListItem("Omzet 4", numberFormat(info.getOmzet4())));
			list.add(new ListItem("Omzet 5", numberFormat(info.getOmzet5())));
			list.add(new ListItem("Omzet 6", numberFormat(info.getOmzet6())));
			list.add(new ListItem("Omzet 7", numberFormat(info.getOmzet7())));
			list.add(new ListItem("Omzet 8", numberFormat(info.getOmzet8())));
			list.add(new ListItem("Omzet 9", numberFormat(info.getOmzet9())));
			list.add(new ListItem("Omzet 10", numberFormat(info.getOmzet10())));
			list.add(new ListItem("Omzet 11", numberFormat(info.getOmzet11())));
			list.add(new ListItem("Omzet 12", numberFormat(info.getOmzet12())));
			list.add(new ListItem("Omzet Average", numberFormat(info.getOmzet1())));			
			list.add(new ListItem("CIF Number", String.valueOf(info.getCIFNumber())));
			list.add(new ListItem("Loan Number", info.getLoanNumber()));
		}
		
		if (App.DEBUG || !Str.isNullOrEmpty(info.getKtpName())) {
			list.add(new GroupInfo("Detail"));			
			list.add(new ListItem(getString(R.string.identity_number), info.getKtpNo()));			
			list.add(new ListItem(getString(R.string.identity_name), info.getKtpName()));
			//list.add(new ListItem(getString(R.string.identity_address), info.getKtpAddress()));
			String bDay = "";
			if (!Str.isNullOrEmpty(info.getDateOfBirth())) {
				String[] aBirthDay = info.getDateOfBirth().split(" ");
				bDay = DialogUtil.changeFormat(aBirthDay[0]);
			}
			
			list.add(new ListItem(getString(R.string.birthdate), bDay));
			list.add(new ListItem(getString(R.string.place_of_birth), info.getPlaceOfBirth()));						
			list.add(new ListItem(getString(R.string.marital_status), info.getMaritalStatus()));
			if (StandardDB.MARITAL_STATUS_MARIED.equals(info.getMaritalStatus()))
				list.add(new ListItem(getString(R.string.spouse_name), info.getSpouseName()));
			list.add(new ListItem(getString(R.string.avg_expense), numberFormat(info.getAvgExpense())));
			list.add(new ListItem(getString(R.string.number_of_dependents), numberFormat(info.getJmlTanggungan())));			
			list.add(new ListItem(getString(R.string.product), info.getProductCode()));	
				
		}
		
		/*
		if (App.DEBUG) {			
			list.add(new GroupInfo("History"));
			list.add(new ListItem("Source Code", String.valueOf(info.getSourceCode())));
			list.add(new ListItem("Entry Date", info.getEntryDate()));			
			list.add(new ListItem("Update Date", info.getUpdateDate()));
			list.add(new ListItem("Is Upload", String.valueOf(info.getIsUpload())));
			list.add(new ListItem("Upload By", String.valueOf(info.getUploadBy())));
			list.add(new ListItem("Upload Date", String.valueOf(info.getUploadDate())));			
			
			list.add(new ListItem("Pipeline Start Time", String.valueOf(info.getPipelineStartTime())));			
			list.add(new ListItem("Pipeline Finish Time", String.valueOf(info.getPipelineFinishTime())));
			list.add(new ListItem("Prospect Start Time", String.valueOf(info.getProspectStartTime())));
			list.add(new ListItem("Prospect Finish Time", String.valueOf(info.getProspectFinishTime())));			
			
			list.add(new ListItem("Is Pipeline KP", String.valueOf(info.getIsPipelineKP())));
			
			list.add(new ListItem("GU FUP", String.valueOf(info.getGuFup())));
			list.add(new ListItem("GU Close Deal", String.valueOf(info.getGuCloseDeal())));	
			list.add(new ListItem("Flag Freeze", String.valueOf(info.getFlagFreeze())));	
			list.add(new ListItem("Inbox", String.valueOf(info.isInbox())));
			list.add(new ListItem("NIP", info.getNIP()));
		}*/		
		
		GroupAdapter adapter = new GroupAdapter(getActivity(), list);
		lst.setAdapter(adapter);			
	}
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.pipeline_view_fragment, container, false);
		txtName = (TextView) v.findViewById(R.id.txtName);						
		lst = (ListView) v.findViewById(R.id.lstGroup);						
		
		return v;
	}
	

}
